﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SpriteSample
{
    public class animation
    {
        private float m_interval, m_localtime;
        private int m_firstframe, m_lastframe, m_curframe;

        #region get and set
        public int FirstFrame
        {
            get { return m_firstframe; }
            set { m_firstframe = value; }
        }
        public int LastFrame
        {
            get { return m_lastframe; }
            set { m_lastframe = value; }
        }
        public int CurentFrame
        {
            get { return m_curframe; }
            set { m_curframe = value; }
        }
        public float Interval
        {
            get { return m_interval; }
            set { m_interval = value; }
        }
        public float Localtime
        {
            get { return m_localtime; }
            set { m_localtime = value; }
        }
        #endregion

        public animation()
        {

        }

        public animation(animation _copy)
        {
            m_firstframe = _copy.m_firstframe;
            m_lastframe = _copy.m_lastframe;
            m_curframe = _copy.m_curframe;
            m_localtime = _copy.m_localtime;
            m_interval = _copy.m_interval;
        }
        public void CreateAnimation(int _ff, int _lf, float _itv)
        {
            m_firstframe = _ff;
            m_lastframe = _lf;
            m_curframe = _ff;
            m_localtime = 0.0f;
            m_interval = _itv;
        }
        public void nextframe()
        {
            m_curframe++;
            if (m_curframe > m_lastframe)
                m_curframe = m_firstframe;
        }
        public void UpdateAnimation(GameTime _Gametime, ref Rectangle _srcRect, int _Width, int _Height, int _Cols)
        {
            Rectangle rect;
            m_localtime += (float)_Gametime.ElapsedGameTime.TotalMilliseconds;
            if (m_localtime >= m_interval)
            {
                nextframe();
                m_localtime = 0;
                rect.X = (m_curframe % _Cols) * _Width;
                rect.Y = (m_curframe / _Cols) * _Height;
                rect.Width = _Width;
                rect.Height = _Height;
                _srcRect = rect;
            }

        }
        
    }
}

